package com.example.test_20210701_FileReader;

import java.io.*;


/**
 * bufferWriter 性能高于 FileWriter
 */
public class FileReaderTest {

    public static void main(String[] args){

//        FileInOutPutTest("C:\\Users\\leizi\\Desktop\\salary接口\\颜值排行榜/20201030170001032.jpg",
//                "src/main/java/com/example/test_20210701_FileReader/美女1.png");
//        BufferWriterTest("C:\\Users\\leizi\\Desktop\\uat1-hrms-emolument-748f4b966f-gbdrt-2021-07-01-16-16-00.log",
//                "src/main/java/com/example/test_20210701_FileReader/log2.txt");
        FileInOutPutTest("C:\\Users\\leizi\\Desktop/IO继承关系图.png",
                "src/main/java/com/example/test_20210701_FileReader/IO继承关系图.png");
    }

    // char[]字符流,读取文本信息效率最高
    private static void BufferWriterTest(String inFileName,String outFileName){
        try (FileReader fileReader = new FileReader(inFileName);
             FileWriter fileWriter = new FileWriter(outFileName,true);
             BufferedReader bufferedReader = new BufferedReader(fileReader);
             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)){
            char[] chars = new char[1024];
            int len;
            while ((len = bufferedReader.read(chars)) != -1){
                bufferedWriter.write(chars,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // char[]字符流,专门用来处理文件
    private static void FileReaderTest(String inFileName,String outFileName){
        try (FileReader fileReader = new FileReader(inFileName);
             FileWriter fileWriter = new FileWriter(outFileName,true)){
            char[] chars = new char[1024];
            int len;
            while ((len = fileReader.read(chars)) != -1){
//                System.out.println(chars);
                fileWriter.write(chars,0,len);
            }
//            fileWriter.write("\n日本鬼子");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // byte[]字节流，全能
    private static void FileInOutPutTest(String inFileName,String outFileName){
        try(FileInputStream fileInputStream = new FileInputStream(inFileName);
            FileOutputStream fileOutputStream = new FileOutputStream(outFileName)){
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1){
                fileOutputStream.write(bytes,0,len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
